<?php
var_dump( 10 % 4);
var_dump( 11 % 4);
var_dump( 12 % 4);
var_dump( 13 % 4);
var_dump( 14 % 4);
var_dump( 15 % 4);
var_dump( 16 % 4);
var_dump( 17 % 4);
var_dump( 18 % 4);
var_dump( 19 % 4);
var_dump( 20 % 4);

/*
$arr = [
    2 => 1,
    5 => 2,
    10 => 3,
    30 => 4,
    60 => 5,
    100 => 6,
    200 => 7,
    500 => 8,
    800 => 9
];


$arr_onsale = array (
    1 => 1,
    2 => 1,
    3 => 1,
    4 => 1,
    5 => 1,
    6 => 1,
    7 => 1,
    8 => 1,
    9 => 1,
);

$money = [
    2   => 10000,
    5   => 25000,
    10  => 50000,
    30  => 170000,
    60  => 350000,
    100 => 625000,
    200 => 1500000,
    500 => 5000000,
    800 => 8000000
];

function Wx_Pay()
{
    header('Access-Control-Allow-Origin: *');
    $uid = input("post.ulist", null, "trim");
    $xid = input("post.xid", null, "trim");
    $fee = input("post.fee", null, "trim");
    $type = input("post.type", null, "trim"); // 0-充值 1-vip 2-道具

    if (empty($uid) || empty($xid)) {
        return $this->ajaxJson(0, null, '数据错了', null);
    }
    $user_model = self::play_xid($xid);
    $uid = alphaID($uid, true, 8);
    $user_info = $user_model->where(['id' => $uid])->find();
    if (empty($user_info)) {
        throw new Exception("用户不存在");
    }
    $d_uid = 0;
    $money = [2 => 10000, 5 => 25000, 10 => 50000, 30 => 170000, 60 => 350000, 100 => 625000, 200 => 1500000, 500 => 5000000, 800 => 8000000];
    $arr_fee = [2 => 1, 5 => 2, 10 => 3, 30 => 4, 60 => 5, 100 => 6, 200 => 7, 500 => 8, 800 => 9];
    $onsale_mod = model('onsale');
    $onsale_user = $onsale_mod->onsalefind(['uid' => $uid, 'xid' => $xid]);
    $arr_onsale = json_decode($onsale_user['onsale'], true);
    if($type==1){
        $starnumber = 0;
    }elseif ($arr_onsale[$arr_fee[$fee]] == 1) { //爱心值翻倍
        $starnumber = $money[$fee] + $money[$fee];
    } else {
        $starnumber = $money[$fee];
    }

    $appid = 'wx6c324e2255e7eef3'; //小程序appid
    $body = $starnumber . '守护心'; // 韵辰
    $mch_id = '1595532861'; //韵辰
    // $mch_id = '1546253341'; //雅悦小馆
    $nonce_str = $this->nonce_str(); //随机字符串
    $notify_url = 'https://xcx.vipxufan.com/star/hpay/nvmingxing_payment'; //回调的url【自己填写】';
    $openid = $user_info['openid']; //'用户的openid【自己填写】';
    $out_trade_no = $this->order_number($openid); //商户订单号
    $spbill_create_ip =  $_SERVER['REMOTE_ADDR']; //'服务器的ip【自己填写】';
    $total_fee = $fee * 100; //因为充值金额最小是1 而且单位为分 如果是充值1元所以这里需要*100
    $trade_type = 'JSAPI'; //交易类型 默认
    if (in_array($uid, [99372,77059])) {
        $total_fee = 1; //举例充值0.01
    }

    //获取会员逻辑
    if($type && $total_fee>1){
        $Vipnew = new Vipnew;
        $vip_level = $Vipnew->queryVip($xid, $uid);
        $cashoff = 1;
        if($vip_level==3){
            $cashoff = 0.8;
        }elseif($vip_level==2){
            $cashoff = 0.9;
        }
        $total_fee = $total_fee * $cashoff ;
    }

    //这里是按照顺序的 因为下面的签名是按照顺序 排序错误 肯定出错
    $post['appid'] = $appid;
    $post['body'] = $body;
    $post['mch_id'] = $mch_id;
    $post['nonce_str'] = $nonce_str; //随机字符串
    $post['notify_url'] = $notify_url;
    $post['openid'] = $openid;
    $post['out_trade_no'] = $out_trade_no;
    $post['spbill_create_ip'] = $spbill_create_ip; //终端的ip
    $post['total_fee'] = $total_fee; //总金额 最低为一块钱 必须是整数
    $post['trade_type'] = $trade_type;

    // if(31){
    // if( $xid==31 ){
    $attach = json_encode([
        "number" => $starnumber,
        "money" => $fee,
    ]);
    $post['attach'] = $attach;//商家数据包，原样返回
    $sign = self::getSign($post); //签名
    $post['sign'] = $sign;
    $post_xml = $this->arrayToXml($post);
    // }else{
    // $sign = $this->sign($post); //签名
    // $post_xml = '<xml>
    //    <appid>' . $appid . '</appid>
    //    <body>' . $body . '</body>
    //    <mch_id>' . $mch_id . '</mch_id>
    //    <nonce_str>' . $nonce_str . '</nonce_str>
    //    <notify_url>' . $notify_url . '</notify_url>
    //    <openid>' . $openid . '</openid>
    //    <out_trade_no>' . $out_trade_no . '</out_trade_no>
    //    <spbill_create_ip>' . $spbill_create_ip . '</spbill_create_ip>
    //    <total_fee>' . $total_fee . '</total_fee>
    //    <trade_type>' . $trade_type . '</trade_type>
    //    <sign>' . $sign . '</sign>
    // </xml> ';
    // }

    //统一接口prepay_id
    $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
    $xml = $this->http_request($url, $post_xml);
    $array = $this->xml($xml); //全要大写
    if ($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS') {
        $time = time();
        $tmp = []; //临时数组用于签名
        $tmp['appId'] = $appid;
        $tmp['nonceStr'] = $nonce_str;
        $tmp['package'] = 'prepay_id=' . $array['PREPAY_ID'];
        $tmp['signType'] = 'MD5';
        $tmp['timeStamp'] = "$time";
        $data['appId'] = $appid;
        $data['state'] = 1;
        $data['timeStamp'] = "$time"; //时间戳
        $data['nonceStr'] = $nonce_str; //随机字符串
        $data['signType'] = 'MD5'; //签名算法，暂支持 MD5
        $data['package'] = 'prepay_id=' . $array['PREPAY_ID']; //统一下单接口返回的 prepay_id 参数值，提交格式如：prepay_id=*

        // if(31){
        // if($xid==31){
        $data['paySign'] = self::getSign($tmp);
        // $data['paySign'] = $this->sign2($tmp);//签名,具体签名方案参见微信公众号支付帮助文档;
        // }else{
        // 	$data['paySign'] = $this->sign($tmp); //签名,具体签名方案参见微信公众号支付帮助文档;
        // }

        $data['out_trade_no'] = $out_trade_no;
        //将数据写入数据库
        $pay_mod = self::pay_model($xid);
        $list = [
            'uid' => $uid,
            'xid' => $xid,
            'code' => 1,
            'openid' => $openid,
            'partner_trade_no' => $out_trade_no,
            'cash_fee' => $fee,
            'real_fee' => $total_fee,
            'prepayid' => $array['PREPAY_ID'],
            'obtain' => (int)$starnumber,
            'status' => 1,
            'type' => $type,
            'return_code' => $array['RESULT_CODE'],
            'mchid' => $mch_id,
            'mch_appid' => $appid,
            'create_time' => date("Y-m-d H:i:s", time())
        ];
        if($d_uid){
            $list['d_uid'] = $d_uid;
        }
        $pay_mod->insert($list);
    } else {
        $data['state'] = 0;
        $data['text'] = "错误";
        $data['RETURN_CODE'] = $array['RETURN_CODE'];
        $data['RETURN_MSG'] = $array['RETURN_MSG'];
        //将数据写入数据库
        $pay_mod = self::pay_model($xid);
        $list = [
            'uid' => $uid,
            'xid' => $xid,
            'code' => 0,
            'openid' => $openid,
            'partner_trade_no' => $out_trade_no,
            'cash_fee' => $fee,
            'obtain' => (int)$starnumber,
            'status' => 3,
            'err_code' => $array['RETURN_CODE'],
            'err_code_des' => $array['RETURN_MSG'],
            'mchid' => $mch_id,
            'mch_appid' => $appid,
            'create_time' => date("Y-m-d H:i:s", time())
        ];
        $pay_mod->insert($list);
    }
    return $this->ajaxJson($data, null, 'ok', null);
}



function play_xid($xid){
    switch ($xid) {
        case 131:
            $uMod = new \app\star\model\Cpuser();
            break;
        case 90:
            $uMod = new \app\star\model\X90user();
            break;
        case 107:
            $uMod = new \app\star\model\X107user();
            break;
        case 31:
            $uMod = new \app\star\model\Idoluser();
            break;
        case 133:
            $uMod = new \app\star\model\X133user();
            break;
        case 3:
            $uMod = new \app\star\model\Xcpuser();
            break;
        case 2:
            $uMod = new \app\star\model\Adcpuser();
            break;
        default:
            $uMod = new \app\star\model\X133user();
            break;
    }
    return $uMod;
}
function pay_model($xid){
    switch ($xid) {
        case 90:
            $pMod = new \app\star\model\Onepay();
            break;
        case 107:
            $pMod = new \app\star\model\Twopay();
            break;
        case 31:
            $pMod = new \app\star\model\Pay();
            break;
        case 131:
            $pMod = new \app\star\model\Cppay();
            break;
        case 133:
            $pMod = new \app\star\model\Femalepay();
            break;
        case 3:
            $pMod = new \app\star\model\Xcppay();
            break;
        case 2:
            $pMod = new \app\star\model\Adcppay();
            break;
        default:
            $pMod = new \app\star\model\Gzhpay();
            break;
    }
    return $pMod;
}


array (
    'appid' => 'wx6c324e2255e7eef3',
    'attach' => '{"number":10000,"money":"2"}',
    'bank_type' => 'OTHERS',
    'cash_fee' => '200',
    'fee_type' => 'CNY',
    'is_subscribe' => 'N',
    'mch_id' => '1595532861',
    'nonce_str' => 'VpZaslOMPErTipfhlKBrDXmTIKqGkieO',
    'openid' => 'o1rP50IPRHgXtKylBwiNDbFiCm48',
    'out_trade_no' => '531b3904ffc383147e09f764e5a1f205',
    'result_code' => 'SUCCESS',
    'return_code' => 'SUCCESS',
    'sign' => '41586CD241C7B12977BA8870862536F9',
    'time_end' => '20210802110851',
    'total_fee' => '200',
    'trade_type' => 'JSAPI',
    'transaction_id' => '4200001144202108023264614428',
);

$arr_onsale[  $arr_fee[  $pay_info['cash_fee']]] = 0;
*/

function pay_model($xid)
{
    switch ($xid) {
        case 90:
            $pMod = new \app\star\model\Onepay();
            break;
        case 107:
            $pMod = new \app\star\model\Twopay();
            break;
        case 31:
            $pMod = new \app\star\model\Pay();
            break;
        case 131:
            $pMod = new \app\star\model\Cppay();
            break;
        case 133:
            $pMod = new \app\star\model\Femalepay();
            break;
        case 3:
            $pMod = new \app\star\model\Xcppay();
            break;
        case 2:
            $pMod = new \app\star\model\Adcppay();
            break;
        default:
            $pMod = new \app\star\model\Gzhpay();
            break;
    }
    return $pMod;
}
